<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>生命周期</title>
</head>

<body>
<!-- 创建test -->
<div id="test"></div>
<!-- 引入核心库 -->
<script src="../js/react.development.js"></script>
<!-- 引入react操作DOM支持库 -->
<script src="../js/react-dom.development.js"></script>
<!-- 引入babel 用于将jsx转为js -->
<script src="../js/babel.min.js"></script>
<!-- 引入prop-types 用于对组件标签属性进行限制 -->
<script src="../js/prop-types.js"></script>
<script type="text/babel">  // 此处引入一定要写babel
//  创建组件
class Life extends React.Component {
  state = {
    opacity: 1,
  }

  /**
   * componentDidMount  组件挂载完毕
   * componentWillUnmount  组件将要卸载
   * unmountComponentAtNode 组件卸载
   * */

  // 组件挂载完毕后调用
  componentDidMount() {
    this.InterVal = setInterval(() => {
      let {opacity} = this.state;
      opacity -= 0.1;
      if (opacity <= 0) {
        opacity = 1;
      }
      this.setState({opacity})
    }, 200)
  }

  componentWillUnmount() {
    //  组件将要卸载
    clearInterval(this.InterVal)
  }

  death = () => {
    //  卸载组件
    ReactDOM.unmountComponentAtNode(document.getElementById("test"))
  }

  render() {
    return (
      <div>
        <h2 style={{opacity: this.state.opacity}}>React学不会咋办呢么</h2>
        <button onClick={this.death}>不活啦</button>
      </div>
    )
  }
}

//渲染组件
ReactDOM.render(<Life/>, document.getElementById("test"))
</script>
</body>

</html>